import {FenceGroup} from "../models/fence-group";
import {Judger} from "../models/judger";
import {Spu} from "../../models/spu";
import {Cell} from "../models/cell";
import {Cart} from "../../models/cart";

Component({
    /** 组件接收参数 */
    properties: {
        spu: Object,
        fences: Array,
        orderWay: String
    },
    /** 组件数据 */
    data: {
        judger: Object,
        fences: Object,
        previewImg: String,
        title: String,
        price: Number,
        discountPrice: Number,
        currentSkuCount: Cart.SKU_MIN_COUNT
    },
    /** 监听器 监听属性变化
     * spu 无规格
     * */
    observers: {
        'spu': function (spu) {
            // 无值 结束
            if (!spu) {
                return
            }
            // 无 spu 直接显示第一个
            if (Spu.inNoSpec(spu)) {
                this.processNoSpec(spu)
            } else {
                this.processHasSpec(spu)
            }
            this.triggerSpecEvent()
        }
    },

    /** 组件方法 */
    methods: {
        /**
         * 无规格
         * @param spu
         */
        processNoSpec(spu) {
            this.setData({
                noSpec: true,
            })
            this.bindSkuData(spu.sku_list[0])
            this.setStockStatus(spu.sku_list[0].stock, this.data.currentSkuCount)
        },
        /**
         * 有规格
         * @param spu
         */
        processHasSpec(spu) {
            const fenceGroup = new FenceGroup(spu)
            fenceGroup.initFence()
            const judger = new Judger(fenceGroup)
            this.data.judger = judger
            // 判断是否有默认sku
            const defaultSku = fenceGroup.getDefaultSku();
            if (defaultSku) {
                this.bindSkuData(defaultSku)
                this.setStockStatus(defaultSku.stock, this.data.currentSkuCount)
            } else {
                this.bindSpuData()
            }
            this.bindTipData()
            this.bindInitData(fenceGroup)
        },
        bindSpuData() {
            const spu = this.properties.spu
            this.setData({
                previewImg: spu.img,
                title: spu.title,
                price: spu.price,
                discountPrice: spu.discount_price
            })
        },
        bindSkuData(sku) {
            console.log('sku==============', sku)
            this.setData({
                previewImg: sku.img,
                title: sku.title,
                price: sku.price,
                discountPrice: sku.discount_price,
                stock: sku.stock
            })
        },
        bindTipData() {
            this.setData({
                skuIntact: this.data.judger.isSkuIntact(),
                currentValues: this.data.judger.getCurrentValues(),
                missingKeys: this.data.judger.getMissingKeys()
            })
        },
        /**
         * 初始化数据
         * @param fenceGroup
         */
        bindInitData(fenceGroup) {
            this.setData({
                fences: fenceGroup.fences,
                skuIntact: this.data.judger.isSkuIntact()
            })
        },
        bindFenceGroupData(fenceGroup) {
            this.setData({
                fences: fenceGroup.fences
            })
        },
        setStockStatus(stock, currentCount) {
            this.setData({
                outStock: this.isOutOfStock(stock, currentCount)
            })
        },
        isOutOfStock(stock, currentCount) {
            return stock < currentCount
        },
        onSelectCount(event) {
            const currentCount = event.detail.count
            this.data.currentSkuCount = currentCount
            if (this.data.judger.isSkuIntact()) {
                const sku = this.data.judger.getDeterminateSku()
                this.setStockStatus(sku.stock, currentCount)
            }
        },
        /**
         * x y
         * 行 列
         * 外层遍历行 里头遍历列
         * @param event
         */
        onCellTap(event) {
            const data = event.detail.cell
            const x = event.detail.x
            const y = event.detail.y

            const cell = new Cell(data.spec)
            cell.status = data.status
            console.log(cell)
            const judger = this.data.judger
            judger.judge(cell, x, y)
            // 有sku完整规格
            const skuIntact = judger.isSkuIntact();
            console.log(skuIntact)
            if (skuIntact) {
                const currentSku = judger.getDeterminateSku()
                this.bindSkuData(currentSku)
                this.setStockStatus(currentSku.stock, this.data.currentSkuCount)
            }
            this.bindTipData()
            this.bindFenceGroupData(judger.fenceGroup)
            this.triggerSpecEvent()
        },
        /**
         * 抛出自定义事件
         */
        triggerSpecEvent() {
            const noSpec = Spu.inNoSpec(this.properties.spu)
            if (noSpec) {
                this.triggerEvent('specChange', {
                    noSpec
                })
            } else {
                this.triggerEvent('specChange', {
                    noSpec: Spu.inNoSpec(this.properties.spu),
                    skuIntact: this.data.judger.isSkuIntact(),
                    currentValues: this.data.judger.getCurrentValues(),
                    missingKeys: this.data.judger.getMissingKeys()
                })
            }
        }
    }
});